#pragma once
#include "EventManager.h"
#include "game/EntityManager.h"
// for recording
#include "Console.h"
/**
========================
Record play
========================
*/
class Recorder;
class Record_f : public ICommand
{
public:
    Record_f() { };
    void Exec(std::string &s);
    void Set(Recorder* r) { m_recorder =r; };
    ~Record_f() {};
private:
    Recorder* m_recorder;
};


/**
========================
Stop play
========================
*/
class StopRecording_f : public ICommand
{
public:
    StopRecording_f() { };
    void Exec(std::string &s);
    void Set(Recorder* r) { m_recorder =r; };
    ~StopRecording_f() {};
private:
        Recorder* m_recorder;
};

/**
========================
Play recording
========================
*/
class PlayRecording_f : public ICommand
{
public:
    PlayRecording_f() { };
    void Exec(std::string &s);
    void Set(Recorder* r) { m_recorder =r; };
    ~PlayRecording_f() {};
private:
        Recorder* m_recorder;
};


/**
================
Recorder Listener
================
*/
class Recorder : public IEventListener
{
public:
    Recorder();
    /* Get the Name of the Listener */
    std::string GetName() { return "recorder"; };
    /** Get Listener Type - Events this listener listens too */
    int GetListenerType() { return LT_ENTITY;/*LT_ALL;*/ };

    /** Update, pop events in play mode */
    void Update(long dt);
    /** Handle an event */
    bool HandleEvent( IEvent* e );

    /** Enable recording */
    void StartRecording(const std::string &fileName="demo1"); 
    /** Stop recording */
    void StopRecording() { Save(); m_record = false; };
    /** Play a Demo */
    void PlayRecording( const std::string &fileName) { 
        StopRecording();
        Load( fileName ); 
        m_playing = true;
    };

    /** Save to file */
    void Save();
    /** Load File */
    void Load(const std::string &fileName);

    virtual ~Recorder();
private:
    // queue of events to record
    typedef std::queue< IEvent > type_EventQueue;
    type_EventQueue     m_events;
    // recording
    bool        m_record;
   // playing back demo
    bool    m_playing;

    // time started
    float   m_startTime;
    // old Time since last frame
    float   m_oldTime;
    // Calculate FPS lag
    float   m_deltaTime;
    // next event Time
    float   m_nextTime;


    // player to record
    struct player_t {
        std::string name;
        std::string state;
        Vector2f    pos;
        Vector2f    vel;
        int         hp;
    } m_player;

    // file name
    std::string m_fileName;
 

    // console functions
    Record_f    record_f;
    PlayRecording_f playrec_f;
    StopRecording_f stoprec_f;
};
